package com.ywu.application.standard.dataSource.source.sql;

import com.ywu.application.standard.dataSource.dto.YwuExecCommonParamDto;
import com.ywu.application.standard.dataSource.entity.YwuDataExecEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName YwuPrepareSqlUtil
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/11/20 13:49
 * @Version 1.0
 **/
@Component
@Slf4j
public class YwuPrepareSqlUtil {

    /**
    * @Author GroundDemo
    * @Description 预处理插入sql
    * @Date 14:35 2024/11/20
    * @Param [ywuDataExecEntity]
    * @return void
    **/
    public void prepareInsertSql(YwuDataExecEntity ywuDataExecEntity, PreparedStatement statement) throws SQLException {
        // 获取待插入的map字段
        List<YwuExecCommonParamDto> fieldParam = ywuDataExecEntity.getFieldParam();
        int index = 1;
        // 按照exec_fields的顺序进行预处理
        prepareField(ywuDataExecEntity, fieldParam, index, statement);
    }

    /**
    * @Author GroundDemo
    * @Description 预处理修改sql
    * @Date 14:54 2024/11/20
    * @Param [ywuDataExecEntity, statement]
    * @return void
    **/
    public void prepareUpdateSql(YwuDataExecEntity ywuDataExecEntity, PreparedStatement statement) throws SQLException {
        // 获取待插入的map字段
        List<YwuExecCommonParamDto> fieldParam = ywuDataExecEntity.getFieldParam();
        List<YwuExecCommonParamDto> conditionParam = ywuDataExecEntity.getConditionParam();
        // 优先构建set字段
        int index = 1;
        index = prepareField(ywuDataExecEntity, fieldParam, index, statement);
        // 构建条件字段
        String[] conditionFieldArray = ywuDataExecEntity.getConditionFields().split(",");
        prepareCondition(conditionFieldArray, conditionParam, index, statement);
    }

    /**
    * @Author GroundDemo
    * @Description 预处理删除sql
    * @Date 15:06 2024/11/20
    * @Param [ywuDataExecEntity, statement]
    * @return void
    **/
    public void prepareDeleteSql(YwuDataExecEntity ywuDataExecEntity, PreparedStatement statement) throws SQLException {
        List<YwuExecCommonParamDto> conditionParam = ywuDataExecEntity.getConditionParam();
        // 优先构建set字段
        int index = 1;
        // 构建条件字段
        String[] conditionFieldArray = ywuDataExecEntity.getConditionFields().split(",");
        prepareCondition(conditionFieldArray, conditionParam, index, statement);
    }

    /**
    * @Author GroundDemo
    * @Description 预处理字段值
    * @Date 14:54 2024/11/20
    * @Param [ywuDataExecEntity, paramList, index, statement]
    * @return int
    **/
    private int prepareField(YwuDataExecEntity ywuDataExecEntity, List<YwuExecCommonParamDto> paramList,
                             int index, PreparedStatement statement) throws SQLException {
        for (String field : ywuDataExecEntity.getExecFields()) {
            String value = getParamValue(paramList, field);
            if (Objects.isNull(value) || "NULL".equalsIgnoreCase(value)) {
                statement.setObject(index, null);
            } else {
                statement.setObject(index, value);
            }
            index++;
        }
        return index;
    }

    /**
    * @Author GroundDemo
    * @Description 遍历入参数组
    * @Date 16:56 2024/11/20
    * @Param [paramList, fieldName]
    * @return java.lang.String
    **/
    private String getParamValue(List<YwuExecCommonParamDto> paramList, String fieldName) {
        String res = null;
        int resIndex = -1;
        for (int index = 0; index < paramList.size(); index++) {
            YwuExecCommonParamDto param = paramList.get(index);
            if (StringUtils.equals(fieldName, param.getFieldName())) {
                res = param.getValue();
                resIndex = index;
                break;
            }
        }
        if (resIndex != -1) {
            paramList.remove(resIndex);
        }
        return res;
    }

    /**
    * @Author GroundDemo
    * @Description 预处理条件值
    * @Date 14:55 2024/11/20
    * @Param [conditionFieldArray, conditionMap, index, statement]
    * @return int
    **/
    public int prepareCondition(String[] conditionFieldArray,  List<YwuExecCommonParamDto> paramList,
                                 int index, PreparedStatement statement) throws SQLException {
        for (String condition : conditionFieldArray)  {
            String value = getParamValue(paramList, condition);
            if (Objects.isNull(value) || "NULL".equalsIgnoreCase(value)) {
                statement.setObject(index, null);
            } else {
                statement.setObject(index, value);
            }
            index++;
        }
        return index;
    }

}
